package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyExpertListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.viewholder.OperateExpertHolder;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 操作我的专家列表
 * Created by zy on 2018/1/14.
 */

public class OperateExpertListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener,
        SwipeRefreshLayout.OnRefreshListener, OperateExpertHolder.DeleteExpertListener {
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    int delPos = -1;//删除标记位
    private List<MyExpertListVO> expertList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_operate_expertlist);
        setTitle("我的专家列表");
        setRightText("添加");
    }

    @Override
    public void initMember() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

//        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new OperateExpertHolder(parent, OperateExpertListActivity.this, OperateExpertListActivity.this);
            }
        });
        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                Map<String, Object> map = new HashMap<>();
                map.put("ExpertVO", ((MyExpertListVO) adapter.getAllData().get(position)).getExpertInfo());

                GotoUtil.gotoActivity(OperateExpertListActivity.this, ExpertsDetailActivity.class, "map", map);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null)
                    return;

                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);
                switch (msg.what) {
                    case MSG.MSG_OPERATE_EXPERTS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_OPERATE_EXPERTS_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_DELETE_EXPERTS_SUCCESS:
                        showToast("删除成功");
                        adapter.remove(delPos);
                        if (adapter.getAllData().size() == 0)
                            UserXML.setIsHaveMyExpert(OperateExpertListActivity.this, "false");
                        else
                            UserXML.setIsHaveMyExpert(OperateExpertListActivity.this, "true");
                        break;
                    case MSG.MSG_DELETE_EXPERTS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        expertList = FastJsonUtil.getListBean(object.toString(), "body", "list", MyExpertListVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(expertList);

        if (expertList != null) {
            if (expertList.size() < limit) {
                adapter.stopMore();
            }
        }

        if (adapter.getAllData().size() == 0)
            UserXML.setIsHaveMyExpert(OperateExpertListActivity.this, "false");
        else
            UserXML.setIsHaveMyExpert(OperateExpertListActivity.this, "true");
    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoForResultActivity(OperateExpertListActivity.this, AddMyExpertsActivity.class, 850, "addCount", adapter.getCount());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 850 && resultCode == RESULT_OK)
            onRefresh();
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessExpert.myExpertList(this, String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @Override
    public void onLoadMore() {
        if (expertList != null && expertList.size() >= limit) {
            page++;
            BusinessExpert.myExpertList(this, String.valueOf(page), String.valueOf(limit), mHandler);
        }
    }

    @Override
    public void deletClick(int pos) {
        this.delPos = pos;
        MyExpertListVO vo = (MyExpertListVO) adapter.getAllData().get(pos);
        BusinessExpert.deleteMyExpert(OperateExpertListActivity.this, vo.getId(), mHandler);
    }
}
